<%@page import="study.itwill.common.PatternChecker"%>
<%@page import="study.itwill.jsp.WebHandler"%>
<%@page import="study.itwill.common.DateTime"%>
<%@page import="study.itwill.team5.admin.AdminItem"%>

<%@page import="study.itwill.team5.board.CommentItem"%>
<%@page import="study.itwill.team5.board.Comment"%>
<%@page import="study.itwill.team5.member.MemberItem"%>
<%@page import="study.itwill.team5.member.Member"%>
<%@page import="study.itwill.team5.board.BoardItem"%>
<%@page import="study.itwill.team5.board.Board"%>
<%@page import="study.itwill.common.DBHandler"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page session="true" %>
<%
	WebHandler web = new WebHandler(request,response, out);
	AdminItem loginInfo = (AdminItem)session.getAttribute("ADMIN_INFO");
	DateTime datetime = DateTime.getInstance();
	
	// 로그인 정보가 없을 경우 로그인 페이지로 이동
	if (loginInfo == null) {
		web.goPage("/italia/login.jsp", "로그인이 필요합니다");
		return;
	}
%>
<%
   String seq = request.getParameter("seq");
 
   // 학생번호 유효성 검사
   if (!PatternChecker.isValue(seq)) {
      out.print("<script type='text/javascript'>alert('파라미터가 없습니다.'); history.back();</script>");
      return;
   }
   if (!PatternChecker.isNum(seq)) {
      out.print("<script type='text/javascript'>alert('파라미터 입력이 잘못됐습니다.'); history.back();</script>");
      return;
   }
 
   int n_seq = Integer.parseInt(seq);
%>
<%@ include file="../dbcon.jsp"%>
<%
	
	/** 게시판 데이터 조회 */
	Board board = new Board();
	BoardItem item = board.viewBoard(n_seq);
	
	// 회원목록 받아오기
	Member member = new Member();
	ArrayList<MemberItem> member_list = member.listMember(0);
	
	// 덧글목록 받아오기
	Comment comment = new Comment();
	ArrayList<CommentItem> comment_list = comment.listComment(0,"" + seq);
	

	if (item == null) {
		out.print("<script type='text/javascript'>alert('데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.')");
		return;
	}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<%@ include file="../head.jsp"%>
<!-- Javascript -->
<script src="/italia/assets/js/jquery.js"></script>
<script src="/italia/assets/js/bootstrap.min.js"></script>
<!-- 형식검사 클래스 참조 -->
<script src="/italia/assets/js/string.js"></script>
</head>
<body>
	<%@ include file="../menu.jsp"%>
	<div class="container">
		<div class="page-header">
			<h1>게시글 보기</h1>
		</div>

		<form id="frmBoardView" class="form-horizontal" role="form"
			method="post" action="">

			<!-- 게시글 일련번호 자동입력 -->
			<input type="hidden" name="board_seq" value="<%=seq%>">


			<fieldset>

				<!-- 입력양식 -->
				<div class="form-group">
					<label for="board_category_seq" class="col-md-2 control-label">게시판
						카테고리</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getCategory_name()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="member_seq" class="col-md-2 control-label">회원</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getUser_name()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="title" class="col-md-2 control-label">제목</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getTitle()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="hit" class="col-md-2 control-label">조회수</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getHit()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="content" class="col-md-2 control-label">내용</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getContent()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="reg_date" class="col-md-2 control-label">등록일자</label>
					<div class="col-md-10">
						<p class="form-control-static"><%= datetime.getDateTimeString(item.getReg_date()) %></p>
					</div>
				</div>

				<div class="form-group">
					<label for="edit_date" class="col-md-2 control-label">수정일자</label>
					<div class="col-md-10">
						<p class="form-control-static"><%= datetime.getDateTimeString(item.getEdit_date()) %></p>
					</div>
				</div>

				<div class="form-group">
					<div class="col-md-offset-2 col-md-10">
						<div class="row">
							<div class="col-md-4">
								<a
									href="listBoard.jsp?category=<%=item.getBoard_category_seq()%>"
									class="btn btn-primary btn-block">목록보기</a>
							</div>
							<div class="col-md-4">
								<button type="submit" id="edit" formaction="editBoard.jsp"
									formmethod="post" class="btn btn-info btn-block">수정하기</button>
							</div>
							<div class="col-md-4">
								<button type="submit" id="delete" formaction="deleteBoardOk.jsp"
									formmethod="post" class="btn btn-danger btn-block">삭제하기</button>
							</div>
						</div>
					</div>
				</div>

			</fieldset>
		</form>

		<hr />

		<div class="col-md-offset-2 col-md-10">
			<ul class="media-list">
				<%
			if (comment_list.size() > 0) {
				for (int i=0; i<comment_list.size(); i++) {
					CommentItem comment_item = comment_list.get(i);
			%>
				<li class="media">
					<div class="media-body">
						<h4 class="media-heading"><%=comment_item.getUser_name()%>&nbsp;&nbsp;&nbsp;
							<small><%= datetime.getDateTimeString(item.getEdit_date()) %>&nbsp;&nbsp;&nbsp;
								<a href="#" id="editcomment"
								onclick="window.open('editComment.jsp?comment_seq=<%=comment_item.getSeq()%>', '', 'width=1000, height=250, scrollbars=no, toolbar=no, menubar=no, status=no, location=no'); return false;">수정</a>&nbsp;/&nbsp;
								<a
								href="deleteCommentOk.jsp?comment_seq=<%=comment_item.getSeq()%>&board_seq=<%=seq%>"
								id="delete<%=comment_item.getSeq()%>">삭제</a></small>
						</h4>
						<script type="text/javascript">
								$(function() {
									// id 속성이 "delete"인 요소에 대한 click이벤트
									$("#delete<%=comment_item.getSeq()%>").click(function() {
										// 사용자에게 물어본다.
										if (!confirm("정말 삭제하시겠습니까?")) {
											// 취소를 선택한 경우 처리 둥단
											return false;
										}
									});
								});
							</script>
						<%=comment_item.getComment()%>
					</div>
				</li>
				<%
				}
			}
			%>
			</ul>
		</div>


		<form id="frmCommentAdd" class="form-horizontal" role="form"
			method="post" action="addCommentOk.jsp">
			<fieldset>

				<!-- 게시판 카테고리와 게시글 일련번호 자동입력 -->
				<input type="hidden" name="board_seq" value="<%=seq%>">

				<div class="form-group">
					<div class="col-md-2">
						<select id="member_seq" name="member_seq" class="form-control">
							<option>덧글작성자</option>
							<%
							if (member_list != null) {
								for (int i=0; i<member_list.size(); i++) {
									MemberItem member_item = member_list.get(i);
						%>
							<option value="<%=member_item.getSeq()%>"><%=member_item.getUser_name()%>
								/
								<%=member_item.getEmail1()%>@<%=member_item.getEmail2()%></option>
							<%			
								}
							}
						%>
						</select>
					</div>
					<div class="col-md-9">
						<textarea class="form-control" rows="3" id="comment"
							placeholder="덧글 내용을 입력하세요" name="comment"></textarea>
					</div>
					<div class="col-md-1">
						<button type="submit" id="comment_write"
							formaction="addCommentOk.jsp" formmethod="post"
							class="btn btn-success btn-block">등록</button>
					</div>
				</div>
			</fieldset>
		</form>
	</div>
	<% DBHandler.disconnect(); %>
	<script type="text/javascript">
		$(function() {
			// id 속성이 "delete"인 요소에 대한 click이벤트
			$("#delete").click(function() {
				// 사용자에게 물어본다.
				if (!confirm("정말 삭제하시겠습니까?")) {
					// 취소를 선택한 경우 처리 둥단
					return false;
				}
			});
			
			$("#frmCommentAdd").submit(function() {
				// 작성자 선택여부 검사
				if ($("select[name='member_seq'] > option:selected").index() < 1) {
					alert("작성자를 선택하세요.");
					$("select[name='member_seq']").focus();
					return false;
				}
				
				// 덧글 입력여부 검사
				if (!$("textarea[name='comment']").val().isValue()) {
					alert("덧글을 입력하세요.");
					$("input[name='comment']").focus();
					return false;
				}
			});
		});
	</script>

</body>
</html>